package com.web_bloks.url.client;

import java.util.HashMap;

public class Helper {

	/**
	 * Extracts the component following the '#'
	 * 
	 * Cases:
	 * 1)   #pagename?query
	 * 2)   ?query#pagename
	 * 
	 * @return
	 */
	public static String getPageNameFromLocation() {
	
		String hs = getHostPageLocation();
		
		return extractHashComponent( hs );
	}
	
	/**
	 * Pulls the 'page name' 
	 */
	public static native String getHostPageLocation()
	  /*-{
	    var s = $doc.location.href;
	    return decodeURI(s);
	  }-*/;

	public static native String decodeURI(String str)
	  /*-{
	    return decodeURI(str);
	  }-*/;

	public static native String decodeURIComponent(String str)
	  /*-{
	    return decodeURIComponent(str);
	  }-*/;

	public static String extractHashComponent(String str) {
		
		String hs = new String( decodeURI(str) );
		
		int bindex = hs.indexOf('#');
		
		if ( bindex == -1) return null;
		
		String p1 = new String( hs.substring(bindex+1, hs.length()) );
	
		// strip the Query part, if any
		int qindex = p1.indexOf('?');
		if ( qindex == -1 ) return p1;
		
		return new String( p1.substring(0, qindex) );
		
	}
	/**
	 * Cases:
	 * 0) http://XYZ                  ==> NULL
	 * 1) http://XYZ#page_name        ==> (page_name,NULL) 
	 * 2) http://XYZ?query            ==> (NULL,query)
	 * 3) http://XYZ#page_name?query  ==> (page_name, query)
	 * 4) http://XYZ?query#page_name  ==> (page_name, NULL)
	 * 
	 * Case #4 is deemed invalid.
	 * 
	 * @param url
	 * @return
	 */
	public static UrlFields extractFieldsFromLocation() {
		
		UrlFields uf = new UrlFields();
		
		String hs = new String( getHostPageLocation() );
	
		String hashStr = null;
		int hindex = hs.indexOf('#');
		
		if ( hindex != -1) {
			hashStr = hs.substring(hindex+1, hs.length());
		} else {
			//CASE#0
			return null;
		}
				
		//Assuming CASE#1, CASE#4
		uf.page = hashStr;
		
		int qindex = hashStr.indexOf('?');
		if ( qindex == -1) {
			//CASE#1, CASE#4
			return uf;
		}

		//CASE #3
		uf.page =  hashStr.substring( 0, qindex );
		uf.query = hashStr.substring( qindex+1 );
		
		return uf;
	}
	/**
	 * Cases:
	 * 1- page_name
	 * 2- page_name?query
	 * 
	 * @param token
	 * @return UrlField
	 */
	public static UrlFields extractFieldsFromHistoryToken(String token) {

		UrlFields uf = new UrlFields();
		
		if (token==null || token.length()==0)
			return null;
	
		String queryStr = null;
		int qindex = token.indexOf('?');
		if ( qindex == -1) {
			//CASE#1
			uf.page = new String(token);
			return uf;
		}
		
		queryStr = new String( token.substring(qindex+1, token.length()) );
		
		//CASE #3
		uf.page = new String( token.substring(0, qindex) );
		uf.query = queryStr;
		
		return uf;
		
	}
	
	public static String extractQueryFromLocation() {
		
		String hs = new String( getHostPageLocation() );
	
		int qindex = hs.indexOf('?');
		if ( qindex == -1) {
			return null;
		}

		return hs.substring(qindex+1, hs.length());
	}
	
	public static String extractComponent(String query, String key) {
		
		if (null==query)
			return null;
		
		String[] parts = query.split("&");

		for(int i=0;i<parts.length;i++){
			int index = parts[i].indexOf("=");
			if (index == -1)
				continue;
			String k = parts[i].substring(0, index);
			String v = parts[i].substring(index+1);
			if ( k == key )
				return v;
		}
		
		return null;
	}
	
	/**
	 * Extracts the [key;value] pairs out of a query string.
	 * 
	 * @param query String
	 * @return HashMap
	 */
	public static HashMap<String,String> extractAndMapQuery(String query) {
		
		if (null == query)
			return null;
		
		String[] pairs = query.split("&");
		if (null==pairs)
			return null;
		
		HashMap<String,String> result = new HashMap<String,String>();
		
		for(int i=0;i<pairs.length;i++) {
			String[] el = pairs[i].split("=");
			if (null==el) continue;
			result.put( el[0] , el[1] );
		}
		
		return result;
	}
	
}//END.